package org.micservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.micservice.ResultMap;
import org.micservice.User;
import org.micservice.mapper.UserMapper;
import org.micservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Value("${server.port}")
    String port;
    @Override
    public ResultMap<List<User>> getUserList() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        List<User> users = userMapper.selectList(queryWrapper);
        ResultMap<List<User> >  map = new ResultMap<>();
        if(users!=null){
            map.setCode(200);
            map.setResult(users);
            map.setMsg(port);
        }
        return map;
    }

    public int addUser(User user) throws Exception {
        int insert = 0;
        try {
            if (user.getAge() < 0 || user.getAge() > 120) {
                throw new Exception("年龄异常");
            }
            insert = userMapper.insert(user);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return insert;

    }
}
